package com.example.hello.controller;

import com.example.hello.entity.Company;
import com.example.hello.entity.Factory;
import com.example.hello.entity.Device;
import com.example.hello.service.AssetService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/assets")
public class AssetController {

    @Autowired
    private AssetService assetService;

    @GetMapping("/enterprises")
    public ResponseEntity<Map<String, Object>> getCompanies(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int limit) {
        PageInfo<Company> pageInfo = assetService.getCompanies(page, limit);
        
        Map<String, Object> response = new HashMap<>();
        response.put("status", "success");
        response.put("message", "获取企业列表成功");
        response.put("data", pageInfo.getList());
        response.put("total", pageInfo.getTotal());
        
        return ResponseEntity.ok(response);
    }

    @GetMapping("/factories")
    public ResponseEntity<Map<String, Object>> getFactories(
            @RequestParam Integer enterprise_id,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int limit) {
        PageInfo<Factory> pageInfo = assetService.getFactories(page, limit, enterprise_id);
        
        Map<String, Object> response = new HashMap<>();
        response.put("status", "success");
        response.put("message", "获取工厂列表成功");
        response.put("data", pageInfo.getList());
        response.put("total", pageInfo.getTotal());
        
        return ResponseEntity.ok(response);
    }

    @GetMapping("/devices")
    public ResponseEntity<Map<String, Object>> getDevices(
            @RequestParam Integer factory_id,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int limit) {
        PageInfo<Device> pageInfo = assetService.getDevices(page, limit, factory_id);
        
        Map<String, Object> response = new HashMap<>();
        response.put("status", "success");
        response.put("message", "获取设备列表成功");
        response.put("data", pageInfo.getList());
        response.put("total", pageInfo.getTotal());
        
        return ResponseEntity.ok(response);
    }
} 